package com.freewater.springboot.demo.aop.api.advices.around;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.util.StopWatch;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Arrays;

public class SimpleAroundAdvice implements MethodInterceptor {

    @Override
    public @Nullable Object invoke(@Nonnull MethodInvocation invocation) throws Throwable {
        // StopWatch 是Spring提供计时的类
        StopWatch stopWatch = new StopWatch();
        System.out.println("\n" + "切点表达式：" + invocation.getStaticPart());
        System.out.println("开始计时类：" + invocation.getThis() + "的" + invocation.getMethod().getName() + "方法");
        stopWatch.start(invocation.getMethod().getName());
        // 执行方法
        Object proceed = invocation.proceed();
        System.out.println("\n" + "共打印了" + Arrays.stream(invocation.getArguments()).findFirst().get() + "次");
        stopWatch.stop();
        System.out.println("执行任务共耗时：" + stopWatch.getTotalTimeSeconds());
        return proceed;
    }
}
